//
//  PhotoCollectionViewCell.m
//  NBTripe
//
//  Created by Lee on 16/1/14.
//  Copyright © 2016年 Vicki. All rights reserved.
//

#import "PhotoCollectionViewCell.h"

@interface PhotoCollectionViewCell ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scroview;

@property (nonatomic, strong) UILabel *writingLabel;

@property (nonatomic, strong) UIButton *backButton;

@property (nonatomic, strong) UILabel *cityLabel;

@property (nonatomic, strong) UILabel *timeLabel;

//判断循环播放状态
@property (nonatomic, assign) BOOL judge;

@end

@implementation PhotoCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor blackColor];
        //图片
        self.picimageview = [[UIImageView alloc]  initWithFrame:CGRectZero];
        //图片偏移
        self.pic_imageviewscroview = [[UIScrollView alloc] initWithFrame:CGRectZero];
        self.pic_imageviewscroview.userInteractionEnabled = YES;
        self.pic_imageviewscroview.maximumZoomScale = 2;
        self.pic_imageviewscroview.minimumZoomScale = 0.5;
        self.pic_imageviewscroview.delegate = self;
        //文字偏移
        self.scroview = [[UIScrollView alloc]  initWithFrame:CGRectZero];
        self.scroview.delegate = self;
        //评论
        self.writingLabel = [[UILabel alloc]  initWithFrame:CGRectZero];
        [Convenience createCellLabel:self.writingLabel LabelWithTextColor:[UIColor whiteColor] TextColorWithFont:[UIFont systemFontOfSize:15] FontWithNSTextAlignment:NSTextAlignmentLeft TextAlignmentWithNumberOfLines:0];
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [Convenience createCellLabel:self.titleLabel LabelWithTextColor:[UIColor whiteColor] TextColorWithFont:[UIFont systemFontOfSize:18] FontWithNSTextAlignment:NSTextAlignmentCenter TextAlignmentWithNumberOfLines:1];
        //返回
        self.backButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.backButton setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
        [self.backButton addTarget:self action:@selector(returnBack) forControlEvents:UIControlEventTouchUpInside];
        //时间
        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [Convenience createCellLabel:self.timeLabel LabelWithTextColor:[UIColor whiteColor] TextColorWithFont:[UIFont systemFontOfSize:14] FontWithNSTextAlignment:NSTextAlignmentLeft TextAlignmentWithNumberOfLines:0];
        //城市
        self.cityLabel.backgroundColor = [UIColor orangeColor];
        self.cityLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        [Convenience createCellLabel:self.cityLabel LabelWithTextColor:[UIColor whiteColor] TextColorWithFont:[UIFont systemFontOfSize:14] FontWithNSTextAlignment:NSTextAlignmentLeft TextAlignmentWithNumberOfLines:0];
        //自动播放
        self.playButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.playButton addTarget:self action:@selector(didPressPlay) forControlEvents:UIControlEventTouchUpInside];
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"bofang"] forState:UIControlStateNormal];
        
        [self addSubview:self.pic_imageviewscroview];
        [self addSubview:self.cityLabel];
        [self addSubview:self.timeLabel];
        [self addSubview:self.backButton];
        [self addSubview:self.titleLabel];
        [self.pic_imageviewscroview addSubview:self.picimageview];
        [self addSubview:self.scroview];
        [self addSubview:self.playButton];
        [self.scroview addSubview:self.writingLabel];
    }
    return self;
}
- (void)layoutSubviews {
    [super layoutSubviews];
    self.pic_imageviewscroview.frame = CGRectMake(0, 120*XHIGHT, WIDTH, HIGHT-300*XHIGHT);
    self.picimageview.frame = CGRectMake(0, 0, self.pic_imageviewscroview.width, self.pic_imageviewscroview.height);
    self.scroview.frame = CGRectMake(5*XWIDTH, self.pic_imageviewscroview.bottom+20*XHIGHT, WIDTH-10, 80*XHIGHT);
    //自适应高度
    CGRect rect = [self.writingLabel.text boundingRectWithSize:CGSizeMake(self.scroview.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil];
    self.writingLabel.frame = CGRectMake(0, 0, self.scroview.width, rect.size.height);
    self.scroview.contentSize = CGSizeMake(0, rect.size.height+10);
    self.backButton.frame = CGRectMake(10*XWIDTH, 20*XHIGHT, 20*XWIDTH, 20*XHIGHT);
    self.titleLabel.frame = CGRectMake(0, self.backButton.height, WIDTH, 30*XHIGHT);
    self.cityLabel.frame = CGRectMake(10*XWIDTH, self.scroview.bottom+25*XHIGHT, WIDTH/2-20*XWIDTH, 30*XHIGHT);
    self.timeLabel.frame = CGRectMake(self.cityLabel.right+10*XWIDTH, self.cityLabel.origin.y, self.cityLabel.width, self.cityLabel.height);
    self.playButton.frame = CGRectMake(WIDTH-30*XWIDTH, self.backButton.origin.y, self.backButton.width+5, self.backButton.height+5);
 

}
- (void)setPhotomodel:(CustomDaysWayPointsModel *)photomodel {
    if (_photomodel != photomodel) {
        _photomodel = photomodel;
    }
    [self.picimageview sd_setImageWithURL:[NSURL URLWithString:_photomodel.photo_1600] placeholderImage:[UIImage imageNamed:@"160"]];
    self.writingLabel.text = _photomodel.text;
    self.cityLabel.text = [NSString stringWithFormat:@"🏡 %@", _photomodel.city];
    self.timeLabel.text = [NSString stringWithFormat:@"⏲️ %@", _photomodel.local_time];
}
//点击button返回上一层
- (void)returnBack {

    self.block();
}
//点击button循环播放以及停止循环播放
- (void)didPressPlay {
    if (!_judge) {
     self.playblock();
    }
    else {
        
      self.pauseblock();
    }
    _judge = !_judge;
    
}
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return _picimageview;
}





@end
